import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import '../services/database_service.dart';

class StatisticsScreen extends StatefulWidget {
  @override
  _StatisticsScreenState createState() => _StatisticsScreenState();
}

class _StatisticsScreenState extends State<StatisticsScreen> {
  final DatabaseService _databaseService = DatabaseService();
  Map<String, dynamic> _statistics = {};

  @override
  void initState() {
    super.initState();
    _loadStatistics();
  }

  Future<void> _loadStatistics() async {
    final stats = await _databaseService.getStatistics();
    setState(() {
      _statistics = stats;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('学习统计'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            Text('总单词数: ${_statistics['totalWords'] ?? 0}'),
            Text('已掌握单词数: ${_statistics['masteredWords'] ?? 0}'),
            Text('需复习单词数: ${_statistics['needReviewWords'] ?? 0}'),
            SizedBox(height: 20),
            Expanded(
              child: PieChart(
                PieChartData(
                  sections: [
                    PieChartSectionData(
                      value: (_statistics['masteredWords'] ?? 0).toDouble(),
                      color: Colors.green,
                      title: '已掌握',
                      radius: 50,
                    ),
                    PieChartSectionData(
                      value: (_statistics['needReviewWords'] ?? 0).toDouble(),
                      color: Colors.red,
                      title: '需复习',
                      radius: 50,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
